package com.wteam.dragon.shop.controller;

import com.wteam.dragon.shop.pojo.bean.ProductSort;
import com.wteam.dragon.shop.pojo.vo.ProductSortVo;
import com.wteam.dragon.shop.service.ProductSortService;
import com.wteam.dragon.system.aop.log.log.Log;
import com.wteam.dragon.system.apps.security.annotation.AnonymousAccess;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import com.wteam.dragon.system.util.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 陈晓辉
 * @Date 2020/7/2 11:18
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "商品分类")
@RequestMapping("productSort")
public class ProductSortController {
    private final ProductSortService productSortService;

    @Log("添加商品分类")
    @ApiOperation("添加商品分类")
    @PostMapping("addProductSort")
    @PreAuthorize("@el.check('addProductSort')")
    public ResultMessage addProductSort(@Validated ProductSortVo productSortVo){
        ProductSort productSort = BeanUtil.from(productSortVo, ProductSort.class);
        return productSortService.addProductSort(productSort);
    }

    @Log("删除商品分类")
    @ApiOperation("删除商品分类")
    @DeleteMapping("delProductSort")
    @PreAuthorize("@el.check('delProductSort')")
    public ResultMessage addProductSort(Long id){
        return productSortService.delProductSort(id);
    }

    @Log("更改商品分类")
    @ApiOperation("更改商品分类")
    @PutMapping("editProductSort")
    @PreAuthorize("@el.check('editProductSort')")
    public ResultMessage editProductSort(@Validated ProductSortVo productSortVo){
        ProductSort productSort = BeanUtil.from(productSortVo, ProductSort.class);
        return productSortService.editProductSort(productSort);
    }

    @Log("获得商品分类列表(一级)")
    @ApiOperation("获得商品分类列表(一级)")
    @GetMapping("listParent")
    @PreAuthorize("@el.check('listParent')")
    public ResultMessage listParent(){
        return productSortService.listParent();
    }

    @Log("通过id获得子菜单")
    @ApiOperation("通过id获得子菜单")
    @GetMapping("listSon")
    @PreAuthorize("@el.check('listSon')")
    public ResultMessage listSon(Long id){
        return productSortService.listSon(id);
    }

    @Log("通过id获得分类详情")
    @ApiOperation("通过id获得分类详情")
    @GetMapping("getProductSort")
    @PreAuthorize("@el.check('getProductSort')")
    public ResultMessage getProductSort(Long id){
        return productSortService.getProductSort(id);
    }

    @Log("获得导航栏数据")
    @ApiOperation("获得导航栏数据")
    @GetMapping("getBar")
    @AnonymousAccess
    public ResultMessage getBar(){
        return productSortService.getBar();
    }

}
